package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 需求计划变新增/更流程提交后调用接口变更
 * @ModifyLog:
 **/
public class WeaXqjhxzbglcChangeAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        writeLog(">>>>>WeaXqjhxzbglcChangeAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String yprdh = Util.null2String(rs.getString("yprdh"));
            String bglx = Util.null2String(rs.getString("bglx"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("SERIAL", requestid);
            dataJson.put("OANUM", lcbh);
            dataJson.put("BANFN", yprdh);
            if ("1".equals(bglx)) {
                JSONArray dataitem = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject dataitemJson = new JSONObject();
                    dataitemJson.put("BNFPO", Util.null2String(rs.getString("xxmxh")));
                    dataitemJson.put("BSART", "OA");
                    dataitemJson.put("FRGKZ", Util.null2String(rs.getString("spzt")));
                    dataitemJson.put("EKGRP", Util.null2String(rs.getString("cgz")));
                    dataitemJson.put("ERNAM", Util.null2String(rs.getString("cjdxdrymc")));
                    dataitemJson.put("ERDAT", DateUtil.getFullDate());
                    dataitemJson.put("AFNAM", Util.null2String(rs.getString("xqz")));
                    dataitemJson.put("TXZ01", Util.null2String(rs.getString("wlms")));
                    dataitemJson.put("MATNR", Util.null2String(rs.getString("wlh")));
                    dataitemJson.put("WERKS", Util.null2String(rs.getString("gc")));
                    dataitemJson.put("LGORT", Util.null2String(rs.getString("kcdd")));
                    dataitemJson.put("BEDNR", "");
                    dataitemJson.put("MATKL", Util.null2String(rs.getString("wlz")));
                    dataitemJson.put("MENGE", Util.null2String(rs.getString("sl")));
                    dataitemJson.put("MEINS", Util.null2String(rs.getString("cgsqjldw")));
                    dataitemJson.put("LFDAT", Util.null2String(rs.getString("xqshrq")).replace("-", ""));
                    dataitemJson.put("PREIS", Util.null2String(rs.getString("jey")));
                    dataitemJson.put("PEINH", Util.null2String(rs.getString("jgdw")));
                    dataitemJson.put("PSTYP", Util.null2String(rs.getString("cgpzzdxmlb")));
                    String kmfplb = Util.null2String(rs.getString("kmfplb"));
                    kmfplb = sapUtil.getSelectname(formid, "kmfplb", kmfplb);
                    dataitemJson.put("KNTTP", kmfplb);
                    dataitemJson.put("EKORG", Util.null2String(rs.getString("cgzz")));
                    dataitemJson.put("KOSTL", Util.null2String(rs.getString("cbzx")));
                    dataitemJson.put("DISUB_PSPNR", Util.null2String(rs.getString("wbsxmh")));
                    dataitem.add(dataitemJson);
                }
                dataJson.put("ITEM", dataitem);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaXqjhxzbglcChangeAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaXqjhxzbglcChangeAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("MM083", dataJson, dataMap);
                writeLog(">>>>>WeaXqjhxzbglcChangeAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if (!"S".equals(msgType)) {
                        message += outputJson.getString("MSG_TEXT");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            if ("2".equals(bglx)) {
                JSONArray dataitem = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject dataitemJson = new JSONObject();
                    dataitemJson.put("BNFPO", Util.null2String(rs.getString("xxmxh")));
                    dataitemJson.put("EBAKZ", "X");
                    dataitem.add(dataitemJson);
                }
                dataJson.put("ITEM", dataitem);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaXqjhxzbglcChangeAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaXqjhxzbglcChangeAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("MM083", dataJson, dataMap);
                writeLog(">>>>>WeaXqjhxzbglcChangeAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if (!"S".equals(msgType)) {
                        message += outputJson.getString("MSG_TEXT");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXqjhxzbglcChangeAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}